package com.example.teacher;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.database.DatabaseHelper;
import com.example.pojo.Teacher;
import com.example.student.R;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import de.hdodenhof.circleimageview.CircleImageView;


public class ProfileFragmentAdmin extends Fragment {
    private static final int PICK_IMAGE = 1; // 请求码
    private TextView tvName, tvCollege, tvPhone, tvTeacherId,tvOffice,tvAge,tvJob;
    private Button btnEdit;
    private DatabaseHelper databaseHelper;
    private int teacherId; // 当前教师的 ID
    private CircleImageView ivAvatar;

    private ImageButton btnChangeAvatar;
    private ImageButton btnEditPhone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile_admin, container, false);

        // 初始化视图
        tvName = view.findViewById(R.id.tv_name);
        tvTeacherId = view.findViewById(R.id.tv_teacher_id);
        tvPhone = view.findViewById(R.id.tv_phone);
        ivAvatar = view.findViewById(R.id.iv_avatar);
        tvCollege = view.findViewById(R.id.tv_college);
        tvOffice = view.findViewById(R.id.tv_room);
        tvAge = view.findViewById(R.id.tv_age);
        tvJob = view.findViewById(R.id.tv_job);
        btnChangeAvatar = view.findViewById(R.id.btn_change_avatar);
        btnEditPhone = view.findViewById(R.id.btn_edit_phone);

        // 初始化数据库帮助类
        databaseHelper = new DatabaseHelper(getActivity());

        // 获取当前教师的 ID（假设从 Activity 传递过来）
        Bundle bundle = getArguments();
        if (bundle != null) {
            teacherId = bundle.getInt("teacher_id", -1);
        }
        Log.d("aaa", "onCreateView: teacherId=" +this.teacherId );
        // 加载教师信息
        loadTeacherInfo();

        // 设置头像修改按钮的点击事件
        btnChangeAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();
            }
        });

        // 设置手机号修改按钮的点击事件
        btnEditPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditPhoneDialog();
            }
        });

        return view;
    }

    private void showEditDialog() {

    }

    private void loadTeacherInfo() {
        if (teacherId != -1) {
            Teacher teacher = databaseHelper.getTeacherInfo(teacherId);
            Log.d("aaa", "teacher=" +teacher);
            if (teacher != null) {
                tvName.setText("姓名：" + teacher.getTeacherName());
                tvTeacherId.setText("工号：" + teacher.getTeacherId());
                tvPhone.setText(teacher.getPhone());
                tvJob.setText("职位：" +teacher.getJob());
                tvAge.setText("年龄：" + teacher.getAge());
                tvCollege.setText("学院：" + teacher.getCollege());
                tvOffice.setText("办公室：" + teacher.getOffice());
            }
        }
    }
    // 打开相册选择图片
    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE);
    }

    // 处理选择的图片
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            ivAvatar.setImageURI(selectedImage); // 设置头像
        }
    }

    // 弹出修改手机号的对话框
    private void showEditPhoneDialog() {
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity());
        builder.setTitle("修改手机号");

        // 输入框
        final androidx.appcompat.widget.AppCompatEditText input = new androidx.appcompat.widget.AppCompatEditText(getActivity());
        input.setText(tvPhone.getText().toString());
        builder.setView(input);

        // 保存按钮
        builder.setPositiveButton("保存", (dialog, which) -> {
            String newPhone = input.getText().toString();
            if (!newPhone.isEmpty()) {
                tvPhone.setText(newPhone); // 更新手机号
                // 更新数据库中的手机号
                databaseHelper.updatePhone(teacherId, newPhone, 1);
            }
        });

        // 取消按钮
        builder.setNegativeButton("取消", (dialog, which) -> dialog.cancel());

        builder.show();
    }
}